package com.baixun.gassystem.service.impl;

import com.baixun.gasentity.vo.AlarmStatisticsVO;
import com.baixun.gassystem.mapper.AlarmStatisticsMapper;
import com.baixun.gassystem.service.AlarmStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;

@Service
public class AlarmStatisticsServiceImpl implements AlarmStatisticsService {

    @Autowired
    private AlarmStatisticsMapper alarmStatisticsMapper;

    @Override
    public List<AlarmStatisticsVO> getAlarmStatistics(String startDate, String endDate) {
        // 如果没有传入时间，使用当天日期作为默认值
        if (startDate == null || startDate.isEmpty()) {
            startDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        if (endDate == null || endDate.isEmpty()) {
            endDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }

        // 调用 mapper 查询数据
        List<AlarmStatisticsVO> statistics = alarmStatisticsMapper.selectAlarmStatistics(startDate, endDate);

        // 返回空集合而不是 null，避免空指针异常
        return statistics != null ? statistics : Collections.emptyList(); // 返回一个空的 List
    }
}
